<template>
	<view class="body-content">
		<view class="wap" v-if="false">
			<view class="container">
				<view class="back-img">
					<image src="@/static/img/mine-header-bg.png">
					</image>
				</view>
				<view class="head">
					<view class="item title-item">
						<text class="title">我的积分</text>
					</view>
					<view class="item">
						<text class="balance">{{ info.total_rebate || '0.00' }}</text>
						<view class="tips-icon"></view>
						<view class="balance-tips">
							<text style="margin-right: 7px;">可提现</text>
							<text class="amount" style="margin-right: 7px;">{{ info.able_rebate || '0.00' }}
							</text>
							<text style="margin-right: 7px;">待审核</text>
							<text class="amount">{{ info.verifing_rebate || '0.00' }}</text>
						</view>
					</view>
					<view class="item">
						<view class="button" @click="duihuan()">去提现</view>
					</view>
				</view>
				<view class="body">
					<view class="item head-item border-bottom">
						<view class="left"></view>
						<view class="right">
							<!-- <view class="cell-detail">2021-11</view> -->
							<picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate"
								@change="bindDateChange">
								<view class="cell-detail">{{ date }}</view>
							</picker>
							<image class="icon" src="@/static/img/arrow-bottom-black-ico.png">
							</image>
						</view>
					</view>
					<!-- <view class="item statistics-item border-bottom">
            <view>
              <text>报名收益</text>
              <text>￥0.00</text>
            </view>
            <view>
              <text>提现</text>
              <text>￥0.00</text>
            </view>
          </view> -->
					<view class="item statistics-item border-bottom" v-for="(item,index) in data.data_list"
						:key="index">
						<view>
							<text style="color: #333;" v-if="item.type==3">提现驳回</text>
							<text style="color: #333;" v-else>{{ item.memo }}</text>
							<text style="color: #e74c3c;" v-if="item.type==3&&item.memo">(原因:{{ item.memo }})</text>
							<view>{{ item.createtime }}</view>
						</view>
						<view>
							<text style="color: red;" v-if="item.money<=0">￥{{ item.money }}</text>
							<text v-if="item.money>0">￥{{ item.money }}</text>
						</view>
					</view>


					<view v-if="data.no_list" class="bottom-text">暂无数据</view>
					<view class="bottom-text" v-if="!data.no_list&&data.no_more">我也是有底线的~</view>
					<view class="bottom-text" style="color: #888;" v-if="!data.no_list&&!data.no_more">
						加载中...
					</view>
				</view>
			</view>

		</view>

		<view class="page-header">
			<view class="header-con">
				<view class="con-data">
					<view class="data-one">
						<view>可提现</view>
						<view class="one-num">￥{{ info.able_rebate || '0.00' }}</view>
					</view>
					<view class="data-one">
						<view>待审核</view>
						<view class="one-num">￥{{ info.verifing_rebate || '0.00' }}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="page-nav">
			<picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate" @change="bindDateChange">
				<view class="nav-con">
					<view class="con-item">
						<view class="item-title">
							{{ date }}

							<image class="title-icon" src="@/static/img/arrow-bottom-black-ico.png"></image>
						</view>
					</view>
				</view>
			</picker>
		</view>
		<view class="page-list">
			<view class="list-con">
				<view class="con-title">
					收益明细
				</view>
				<view class="con-item" v-for="(item,index) in data.data_list" :key="index">
					<view class="item-data">
						<view class="data-title" v-if="item.type *1 === 3">
							提现驳回
						</view>
						<view class="data-title" v-else>
							{{ item.memo }}
						</view>
						<view class="data-title" v-if="item.type*1 === 3 && item.memo">
							(原因:{{ item.memo }})
						</view>
						<view class="data-time">
							{{ item.createtime }}
						</view>
					</view>
					<view class="item-com" :class="{'item-com--green' : item.money * 1 < 0}">{{ item.money }}元</view>
				</view>
				<view v-if="data.no_list" class="bottom-text">暂无数据</view>
				<view class="bottom-text" v-if="!data.no_list&&data.no_more">我也是有底线的~</view>
				<view class="bottom-text" style="color: #888;" v-if="!data.no_list&&!data.no_more">
					加载中...
				</view>
			</view>
		</view>
		<button class="page-btn" @click="duihuan">我要提现</button>
		<view class="page-btnFooter"></view>

		<uni-popup ref="popup1" type="center">
			<view class="amount-content">
				<view class="amount-title">请输入提现金额</view>
				<view class="amount-input">
					<input class="amount-input-img" @input="recordMoney" :value="money" placeholder="最低提现1.00元"
						type="number">
					</input>
				</view>
				<view @click="doCash" class="amount-bottom flex-x-center">
					<view class="bottom-item">确认提现</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getRevenueRecord,
		getRebateLog,
		cashApply
	} from '../../apirequest/api.js';
	import {
		getShoukuan,
		shoukuanSubmit
	} from '../../apirequest/api.js';

	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				date: currentDate,
				data: {
					data_list: [],
					no_list: !1,
					no_more: !1,
					current_page: 0,
					loading_more: !0,
				},
				lock: 0,
				money: '',
				info: [],
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onLoad() {
			this.loadDataList();
		},
		onShow() {
			this.loadData();
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function(t) {
			var o = this;
			o.loadDataList(function() {
				o.data.loading_more = !0;
			});
		},
		methods: {
			//获取数据
			loadData: function() {
				getRevenueRecord({}).then((res) => {
					if (res.code == 1) {
						this.info = res.data;
					} else {
						uni.showToast({
							title: res.msg,
							icon: "error"
						})
						setTimeout(function() {
							uni.navigateBack({
								delta: 0
							});
						}, 1000)
					}
				}).catch(err => {
					console.log(err)
				})

			},
			//获取数据列表
			loadDataList: function(f) {
				var o = this;
				if (o.lock) {
					return;
				}
				if (o.data.no_list || o.data.no_more) "function" == typeof t && f();
				else {
					var page = (o.data.current_page || 0) + 1;
					var data = {
						page: page,
						date: o.date,
					}
					o.lock = 1;
					console.log(o.lock)
					getRebateLog(data).then((t) => {
						if (t.code == 1) {
							if (1 == t.data.current_page && !t.data.data.length) {
								o.data.no_list = !0;
								o.data.loading_more = !1
							}
							if (t.data.data && t.data.data.length) {
								o.data.data_list = o.data.data_list || [];
								o.data.data_list = o.data.data_list.concat(t.data.data),
									o.data.current_page = page;
							}
							if (!t.data.has_more) {
								o.data.no_more = !0;
							}

						} else {
							uni.showToast({
								title: '网络繁忙',
								icon: 'error'
							})
						}
						o.lock = 0;
					});
				}
			},
			recordMoney(e) {
				this.money = e.detail.value;
			},
			//确定兑换提现
			doCash: function() {
				let that = this;
				console.log(parseFloat(that.money))
				console.log(that.info.able_rebate)
				if (that.money == '') {
					uni.showToast({
						title: '请输入提现金额',
						icon: "error"
					});
					return;
				}

				if (parseFloat(that.money) < 1) {
					uni.showToast({
						title: '最低提现1元',
						icon: "error"
					});
					return;
				}
				if (parseFloat(that.money) > parseFloat(that.info.able_rebate)) {
					uni.showToast({
						title: '提现金额不足，请重新输入',
						icon: "error"
					});
					return;
				}
				if (that.lock) {
					return;
				}
				that.lock = 1;
				cashApply({
					type: 'rebate',
					money: that.money
				}).then((res) => {
					if (res.code == 1) {
						uni.showToast({
							title: '提交成功，请等待审核',
							icon: "success"
						})
						setTimeout(function() {
							window.location.reload();
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: "error"
						})
						setTimeout(function() {
							that.lock = 0;
						}, 1000)
					}
				}).catch(err => {
					console.log(err)
				})
			},
			duihuan: function() {
				uni.showToast({
					title: '暂未开通'
				})
				return
				// getShoukuan({}).then((res) => {
				// 	if (res.code == 1) {
				// 		if (res.data) {
				// 			let shoukuan_img = res.data.shoukuan_img;
				// 			let alipay_account = res.data.alipay_account;
				// 			if (!shoukuan_img && !alipay_account) {
				// 				uni.showToast({
				// 					title: "请先完善账户信息",
				// 					icon: "error"
				// 				})
				// 				setTimeout(function() {
				// 					uni.navigateTo({
				// 						url: '/pages/payCode/payCode'
				// 					});
				// 				}, 1000)
				// 			} else {
				// 				this.$refs.popup1.open()
				// 			}
				// 		}
				// 	}
				// }).catch(err => {
				// 	console.log(err)
				// })

			},
			bindDateChange: function(e) {
				this.date = e.target.value;
				this.data = {
					data_list: [],
					no_list: !1,
					no_more: !1,
					current_page: 0,
					loading_more: !0,
				};
				this.loadDataList();
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}`;
			}
		}
	}
</script>

<style scoped lang="scss">
	.page-header {
		.header-con {
			background-color: #ff8546;
			padding: 50rpx 50rpx 100rpx;

			.con-data {
				display: flex;
				align-items: center;
				justify-content: space-around;
				text-align: center;

				.data-one {
					font-size: 28rpx;
					color: rgba(255, 255, 255, 0.7);

					.one-num {
						font-size: 40rpx;
						font-weight: bold;
						margin-top: 10rpx;
						color: #ffffff;
					}
				}
			}
		}
	}

	.page-nav {
		.nav-con {
			width: 95%;
			margin: -70rpx auto 0;
			background-color: #ffffff;
			padding: 30rpx;
			border-radius: 20rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-around;
			box-shadow: 0 0 20rpx -5rpx rgba(0, 0, 0, 0.2);

			.con-item {
				.item-img {
					width: 70rpx;
					height: 70rpx;
					margin: 0 auto 10rpx;
					display: block;
				}

				.item-title {
					font-size: 28rpx;
					color: #666666;
					display: flex;
					align-items: center;
					justify-content: center;

					.title-icon {
						width: 30rpx;
						height: 30rpx;
						margin-left: 10rpx;
					}
				}
			}
		}
	}

	.page-list {

		.list-con {
			width: 95%;
			margin: 20rpx auto 0;
			background-color: #ffffff;
			padding: 30rpx;
			border-radius: 20rpx;
			box-sizing: border-box;
			box-shadow: 0 0 20rpx -5rpx rgba(0, 0, 0, 0.2);

			.con-title {
				font-size: 34rpx;
				font-weight: bold;
				color: #333333;
				padding-left: 20rpx;
				border-left: 10rpx solid #ff8546;
				margin-bottom: 30rpx;
			}

			.con-item {
				font-size: 30rpx;
				color: #333333;
				padding: 30rpx 0;
				border-bottom: 2rpx solid #eeeeee;
				display: flex;
				justify-content: space-between;
				align-items: flex-start;

				.item-data {
					font-size: 28rpx;
					color: #999999;

					.data-title {
						font-size: 30rpx;
						color: #333333;
						margin-bottom: 10rpx;
						font-weight: bold;
					}
				}

				.item-com {
					font-size: 34rpx;
					color: #ff5500;
					flex-shrink: 0;
					margin-left: 20rpx;
					font-weight: bold;
				}

				.item-com--green {
					color: #05c475;
				}
			}

		}
	}

	.page-btn {
		width: 492rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: linear-gradient(90deg, #ff8546, #ffb130);
		border-radius: 80rpx;
		font-size: 28rpx;
		color: rgba(#fff, 0.9);
		margin-bottom: 20rpx;
		position: fixed;
		z-index: 9;
		left: 50%;
		bottom: 20rpx;
		transform: translateX(-50%);

		&:after {
			border: none;
		}
	}

	.page-btnFooter {
		height: 160rpx;
	}

	.amount-content {
		background-color: #fff;
		width: 70vw;
		border-radius: 10px;
		overflow: hidden;

		.amount-title {
			text-align: center;
			font-size: 28rpx;
			font-weight: 700;
			padding: 14px 0;
			background-color: #ff8546;
			color: #FFFFFF;
		}

		.amount-input {
			background-color: #f4f5f7;
			width: 90%;
			margin: 30px auto;
			border-radius: 7px;
			height: 50px;
			display: flex;
			align-items: center;

			.amount-input-img {
				width: 100%;
				font-size: 30rpx;
				color: #333;
				padding: 10px;
				text-align: center
			}
		}

		.amount-bottom {
			width: 100%;
			font-size: 28rpx;
			padding: 30rpx 0;
			border-top: 2rpx solid #eeeeee;

			.bottom-item {
				width: 50%;
				background: linear-gradient(90deg, #ff8546, #ffb130);
				text-align: center;
				padding: 10px;
				color: #FFFFFF;
				border-radius: 40rpx;
			}
		}
	}
</style>